#include<bits/stdc++.h>
using namespace std;
#define LN int(1e6+5)
int r[LN]={0},cl[LN]={0};
int hx[LN];
int n;
long long ans=0;
void build(int i,int j,long long deep){
    /*
    cout<<"I:"<<i<<" J:"<<j<<" de:"<<deep<<" HX:";
    for(int i=1;i<=n;i++){
        cout<<" "<<hx[i];
    }
    cout<<endl;
    */
    if(i>n){
        ans=(ans>deep?ans:deep);
        //cout<<"ans:"<<ans<<endl;
        return;
    }
    else {
        if(i==j){
            if(j<n) build(i,j+1,deep);
            else build(i+1,j,deep);
        }
        else{
            if(hx[i]==0){
                if(hx[j]==1){
                    if(j<n) build(i,j+1,deep);
                    else build(i+1,1,deep);
                }
                else {
                    if(r[i]>r[j]){
                        hx[i]=2;
                        int ls=hx[j];
                        hx[j]=1;
                        build(i+1,1,deep+1);
                        hx[i]=0;hx[j]=ls;
                    }
                    else {
                        if(j<n) build(i,j+1,deep);
                        else build(i+1,1,deep);
                    }
                }
            }
            else {
                if(i<=n) build(i+1,1,deep);
               // else return;
            }
        }
    }
    return ;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    freopen("duel.in","r",stdin);
    freopen("duel.out","w",stdout);
    cin>>n;
    memset(hx,0,sizeof hx);
    for(int i=1;i<=n;i++){
        cin>>r[i];
        //cl[i]=r[i];
    }
    build(1,1,0);
    //for(int i=1;i<=n;i++){
    //    cout<<" "<<hx[i];
    //}
    cout<<(n-ans)<<endl;
    fclose(stdin);
    fclose(stdout);
    return 0;
}